/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package handler;

import dao.PostDao;
import entidades.Post;
import entidades.Usuario;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.enterprise.context.SessionScoped;
import javax.faces.context.FacesContext;
import javax.inject.Named;

/**
 *
 * @author PARTICULAR
 */
@Named(value = "postHandler")
@SessionScoped
public class PostHandler implements Serializable {

    private Post p = new Post();
    private Usuario u;
    private PostDao dao = new PostDao();
    private List<Post> posts = new ArrayList<Post>();
    private Post deletar;

    public PostHandler() throws IOException {
        try {
            u = (Usuario) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("usuario");
            System.out.println("oooooo" + u.getNome());
            p = new Post();
            p.setId_usuario(u);
            
        } finally {
            FacesContext.getCurrentInstance().getExternalContext().redirect("LoginInvalido.xhtml");

        }
        //listaPosts();
    }

    public List<Post> getPosts() {
        return posts;
    }

    public void setPosts(List<Post> posts) {
        this.posts = posts;
    }

    public Post getP() {
        return p;
    }

    public void setP(Post p) {
        this.p = p;
    }

    public void salvar() {
        dao.inserir(p);
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("Home.xhtml");
        } catch (IOException ex) {
            Logger.getLogger(PostHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public List<Post> listaPosts() {
        posts = dao.getAll();
        return posts;
    }
    public void apagar(){
        dao.excluir(deletar);
    
    }
    
    
}
